package freakin.wify;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class MapManager extends ItemizedOverlay {

	protected ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

	protected OverlayItem networkOverlay;

	private Context mContext;
	
	private Drawable marker;

	public MapManager(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		marker = boundCenterBottom(defaultMarker);
		populate();
	}

	public MapManager(Drawable defaultMarker, Context context) {
		super(defaultMarker);
		marker = boundCenterBottom(defaultMarker);
		mContext = context;
		populate();
	}

	public void setNetworkOverlay(OverlayItem networkPosition) {
		if (networkPosition != null && !networkPosition.equals(networkOverlay)) {
			mOverlays.remove(networkOverlay);
			networkOverlay = networkPosition;
			addOverlay(networkOverlay);
		}
	}

	private void addOverlay(OverlayItem overlay) {
		overlay.setMarker(marker);
		mOverlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	@Override
	protected boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}

}
